﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using WcfAfvandingServiceLibrary;
using System.ServiceModel;
using System.Collections.ObjectModel;
using System.Threading;

namespace AfvandingSystem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        IAfvandingL afvanding;
        Kanal kanalDTO;

        public MainWindow()
        {
            InitializeComponent();
            ChannelFactory<IAfvandingL> cfAfvanding = new ChannelFactory<IAfvandingL>(new NetTcpBinding(), Constants.URI);
            afvanding = cfAfvanding.CreateChannel();
            kanalDTO = new Kanal();

            lvPumpestationer.ItemsSource = null;
            lvPumpestationer.ItemsSource = afvanding.MinePumpestationer();
        }

        private void lvPumpestationer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvPumpestationer.SelectedItem != null)
            {
                Pumpestation pumpestation = (Pumpestation)lvPumpestationer.SelectedItem;

                lvPumpe.ItemsSource = afvanding.MinePumper(pumpestation.PumpestationsId).ToList();
            }
        }

        private void btnOpretPumpeStation_Click(object sender, RoutedEventArgs e)
        {
            OpretPumpstation pumpestationvindue = new OpretPumpstation(afvanding);
            pumpestationvindue.ShowDialog();
            lvPumpestationer.ItemsSource = afvanding.MinePumpestationer();
        }

        private void btnOpretPumpe_Click(object sender, RoutedEventArgs e)
        {
            OpretPumpe pumpevindue = new OpretPumpe(afvanding);

            pumpevindue.ShowDialog();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (lvPumpe.SelectedItem != null && ((Pumpe)lvPumpe.SelectedItem).Kører != true)
            {
                int pumpeId = ((Pumpe)lvPumpe.SelectedItem).PumpeId;
                int pumpestationsId = ((Pumpestation)lvPumpestationer.SelectedItem).PumpestationsId;
                //MessageBox.Show(afvanding.StartPumpe(pumpeId, pumpestationsId));
            }

            ((Pumpe)lvPumpe.SelectedItem).Kører = true;
                textBox1.Text = kanalDTO.Vandstand.ToString();
        }

        private void btnSletPumpestation_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int pumpestationsId = ((Pumpestation)lvPumpestationer.SelectedItem).PumpestationsId;
                afvanding.SletPumpestation(pumpestationsId);
                lvPumpestationer.ItemsSource = afvanding.MinePumpestationer();
                MessageBox.Show("Pumpstationen: " + pumpestationsId + " er nu slettet");
            }
            catch
            {
                MessageBox.Show("Pumpstationen indenholder en eller flere pumper.");
            }
        }

        private void btnSletPumpe_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int pumpeId = ((Pumpe)lvPumpe.SelectedItem).PumpeId;
                int pumpestationsId = ((Pumpestation)lvPumpestationer.SelectedItem).PumpestationsId;
                afvanding.SletPumpe(pumpeId);
                lvPumpe.ItemsSource = afvanding.MinePumper(pumpestationsId);
                MessageBox.Show("Pumpen: " + pumpeId + " er nu slettet");
            }
            catch
            {
                MessageBox.Show("Pumpen eksistere ikke.");
            }
        }
    }
}
